package com.ruoyi.project.im.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.im.domain.ImMessage;
import com.ruoyi.project.im.mapper.ImMessageMapper;
import com.ruoyi.project.im.mapper.ImTemporaryMessageMapper;
import com.ruoyi.project.im.service.IImMessageService;
import com.ruoyi.project.im.vo.MessageVo;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 消息记录Controller
 *
 * @author hongwj
 * @date 2024-03-01
 */
@RestController
@RequestMapping("/im/message")
@Slf4j
public class ImMessageController extends BaseController {
    @Autowired
    private IImMessageService imMessageService;

    @Autowired
    private ImMessageMapper messageMapper;

    @Autowired
    private ImTemporaryMessageMapper temporaryMessageMapper;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询消息记录列表
     */
    @PreAuthorize("@ss.hasPermi('im:message:list')")
    @GetMapping("/list")
    public TableDataInfo list(ImMessage imMessage) {
        startPage();
        List<ImMessage> list = imMessageService.selectImMessageList(imMessage);
        return getDataTable(list);
    }

    /**
     * 查询所有消息记录列表
     */
    @PreAuthorize("@ss.hasPermi('im:message:all')")
    @Log(title = "消息记录", businessType = BusinessType.OTHER)
    @GetMapping("/all/messages")
    public AjaxResult all(ImMessage imMessage) {
        List<ImMessage> list = imMessageService.selectImMessageList(imMessage);
        return success(list);
    }

    /**
     * 导出消息记录列表
     */
    @PreAuthorize("@ss.hasPermi('im:message:export')")
    @Log(title = "消息记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ImMessage imMessage) {
        List<ImMessage> list = imMessageService.selectImMessageList(imMessage);
        ExcelUtil<ImMessage> util = new ExcelUtil<ImMessage>(ImMessage.class);
        util.exportExcel(response, list, "消息记录数据");
    }

    /**
     * 获取消息记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('im:message:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(imMessageService.selectImMessageById(id));
    }

    /**
     * 新增消息记录
     */
    @Log(title = "消息记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ImMessage imMessage) {
        return toAjax(imMessageService.insertImMessage(imMessage));
    }

    /**
     * 修改消息记录
     */
    @PreAuthorize("@ss.hasPermi('im:message:edit')")
    @Log(title = "消息记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ImMessage imMessage) {
        return toAjax(imMessageService.updateImMessage(imMessage));
    }

    /**
     * 删除消息记录
     */
    @PreAuthorize("@ss.hasPermi('im:message:remove')")
    @Log(title = "消息记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(imMessageService.deleteImMessageByIds(ids));
    }


    /**
     * 查询消息记录列表
     */
    @GetMapping("/queryPrivateMessage")
    public TableDataInfo queryPrivateMessage(Long chatUserId) {
        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        SysUser chatUser = userService.selectUserById(chatUserId);
        startPage();
        List<MessageVo> list = messageMapper.queryPrivateMessage(SecurityUtils.getUserId(), chatUserId);
        for (MessageVo messageVo : list) {
            if (messageVo.isMe()) {
                messageVo.setAvatar(currentUser.getAvatar());
            } else {
                messageVo.setAvatar(chatUser.getAvatar());
            }
        }
        return getDataTable(list);
    }


    @Log(title = "将发送失败的临时消息删除", businessType = BusinessType.UPDATE)
    @PutMapping("/deletePrivateTemporaryMessage")
    public AjaxResult deletePrivateTemporaryMessage(@RequestBody Map<String, Long> map) {
        temporaryMessageMapper.deletePrivateTemporaryMessage(SecurityUtils.getUserId(), map.get("chatUserId"));
        return AjaxResult.success();
    }


    @GetMapping("/queryPrivateTemporaryMessage")
    public AjaxResult queryPrivateTemporaryMessage(Long chatUserId) {
        return success(imMessageService.queryPrivateTemporaryMessage(SecurityUtils.getUserId(), chatUserId));
    }

    /**
     * 查询用户聊天记录好友列表
     */
    @Log(title = "查询聊天好友", businessType = BusinessType.OTHER)
    @GetMapping("/queryChatUserMessage")
    public AjaxResult queryChatUserMessage() {
        return success(imMessageService.queryChatUserMessage());
    }

}
